﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TaskFactoryExtensions.ContinueWhenAll.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the TaskFactoryExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System;
    using System.Collections.ObjectModel;
    using System.Threading;

    public static partial class TaskFactoryExtensions
    {
        private class TaskSetContinuationSource : ContinuationSource
        {
            #region Fields

            private readonly Predicate<TaskSetContinuationSource> _predicate;
            private int _completedCount;
            private readonly ReadOnlyCollection<ITask> _tasks;

            #endregion

            public TaskSetContinuationSource(ITask[] tasks,
               Predicate<TaskSetContinuationSource> predicate)
            {
                #region Validate Arguments

                if (tasks == null)
                {
                    throw new ArgumentNullException("tasks");
                }

                if (predicate == null)
                {
                    throw new ArgumentNullException("predicate");
                }

                #endregion

                _tasks = new ReadOnlyCollection<ITask>((ITask[])tasks.Clone());
                foreach (var task in _tasks)
                {
                    task.ContinuationSource.ContinueWith(TaskCompleted);
                }
                _predicate = predicate;

                UpdateCompleted();
            }

            public ReadOnlyCollection<ITask> Tasks
            {
                get
                {
                    return _tasks;
                }
            }

            private void TaskCompleted()
            {
                Interlocked.Increment(ref _completedCount);
                UpdateCompleted();
            }

            private void UpdateCompleted()
            {
                if (!IsComplete && _predicate(this))
                {
                    Completed();
                }
            }

            public int CompletedCount
            {
                get { return _completedCount; }
            }
        }

        public static ITask ContinueWhenAll(this TaskFactory instance, ITask[] tasks, Action<ITask[]> continuationAction)
        {
            var task = instance.From(continuationAction, tasks);
            task.ContinueFrom(new TaskSetContinuationSource(tasks,
               s => s.Tasks.Count == s.CompletedCount));
            return task;
        }

        public static ITask ContinueWhenAny(this TaskFactory instance, ITask[] tasks, Action<ITask[]> continuationAction)
        {
            var task = instance.From(continuationAction, tasks);
            task.ContinueFrom(new TaskSetContinuationSource(tasks,
               s => s.CompletedCount > 0));
            return task;
        }
    }
}
